layout.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import "@/styles/globals.css";
  2. import type { Metadata, Viewport } from "next";
  3. import { notFound } from "next/navigation";
  4. import { Analytics } from "@vercel/analytics/next";
  5. import { ThemeProvider } from "next-themes";
  6. import { Backdrop } from "@/components/backdrop/backdrop";
  7. import { CursorLens } from "@/components/glass/cursor-lens";
  8. import { SiteNav } from "@/components/nav/site-nav";
  9. import { SiteFooter } from "@/components/site-footer";
  10. import { UpdateOnReturn } from "@/components/update-on-return";
  11. import { FontLoader } from "@/components/font-loader";
  12. import { LocaleProvider } from "@/components/locale-provider";
  13. import {
  14. deferredFaces,
  15. fontDisplay,
  16. fontDisplayZh,
  17. fontName,
  18. fontText,
  19. } from "@/config/fonts";
  20. import { siteConfig } from "@/config/site";
  21. import {
  22. alternates,
  23. htmlLang,
  24. isLocale,
  25. localePath,
  26. locales,
  27. ogLocale,
  28. type Locale,
  29. } from "@/lib/i18n";
  30. import { getMessages } from "@/messages";
  31. export function generateStaticParams() {
  32. return locales.map((lang) => ({ lang }));
  33. }
  34. type Props = { params: Promise<{ lang: string }> };
  35. export async function generateMetadata({ params }: Props): Promise<Metadata> {
  36. const { lang } = await params;
  37. if (!isLocale(lang)) return {};
  38. const { meta } = getMessages(lang);
  39. return {
  40. metadataBase: new URL(siteConfig.url),
  41. title: {
  42. default: meta.siteName,
  43. template: `%s - ${meta.siteName}`,
  44. },
  45. description: meta.description,
  46. keywords: siteConfig.keywords,
  47. authors: [{ name: siteConfig.author, url: siteConfig.url }],
  48. creator: siteConfig.author,
  49. alternates: alternates(lang, "/"),
  50. openGraph: {
  51. type: "website",
  52. locale: ogLocale[lang],
  53. alternateLocale: locales
  54. .filter((l) => l !== lang)
  55. .map((l) => ogLocale[l]),
  56. url: localePath(lang, "/"),
  57. siteName: meta.siteName,
  58. title: meta.siteName,
  59. description: meta.description,
  60. },
  61. twitter: {
  62. card: "summary_large_image",
  63. title: meta.siteName,
  64. description: meta.description,
  65. },
  66. robots: {
  67. index: true,
  68. follow: true,
  69. googleBot: {
  70. index: true,
  71. follow: true,
  72. "max-video-preview": -1,
  73. "max-image-preview": "large",
  74. "max-snippet": -1,
  75. },
  76. },
  77. icons: {
  78. icon: "/favicon.ico",
  79. },
  80. };
  81. }
  82. export const viewport: Viewport = {
  83. themeColor: [
  84. { media: "(prefers-color-scheme: light)", color: "#f6f2f2" },
  85. { media: "(prefers-color-scheme: dark)", color: "#0b0f11" },
  86. ],
  87. };
  88. const jsonLd = (lang: Locale) => ({
  89. "@context": "https://schema.org",
  90. "@graph": [
  91. {
  92. "@type": "WebSite",
  93. "@id": `${siteConfig.url}/#website`,
  94. url: siteConfig.url,
  95. name: getMessages(lang).meta.siteName,
  96. description: getMessages(lang).meta.description,
  97. inLanguage: ["en-US", "zh-CN"],
  98. },
  99. {
  100. "@type": "Person",
  101. "@id": `${siteConfig.url}/#person`,
  102. name: "Eric Wu",
  103. alternateName: "Chengxiang Wu",
  104. url: siteConfig.url,
  105. jobTitle: "Software Development Engineer II",
  106. worksFor: {
  107. "@type": "Organization",
  108. name: "Amazon Web Services",
  109. },
  110. alumniOf: [
  111. {
  112. "@type": "CollegeOrUniversity",
  113. name: "University of California, Los Angeles",
  114. },
  115. {
  116. "@type": "CollegeOrUniversity",
  117. name: "Purdue University",
  118. },
  119. ],
  120. sameAs: [siteConfig.links.github, siteConfig.links.linkedin],
  121. },
  122. ],
  123. });
  124. export default async function RootLayout({
  125. children,
  126. params,
  127. }: Props & { children: React.ReactNode }) {
  128. const { lang } = await params;
  129. if (!isLocale(lang)) notFound();
  130. const t = getMessages(lang);
  131. return (
  132. <html
  133. suppressHydrationWarning
  134. className={`${fontText.variable} ${fontDisplay.variable} ${fontName.variable} ${fontDisplayZh.variable}`}
  135. lang={htmlLang[lang]}
  136. >
  137. <head>
  138. {/* Returning visitors have the display face cached: use it from the first frame. */}
  139. <script
  140. dangerouslySetInnerHTML={{
  141. __html:
  142. 'try{localStorage.getItem("ff")&&document.documentElement.classList.add("ff")}catch(e){}',
  143. }}
  144. />
  145. <script
  146. dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd(lang)) }}
  147. type="application/ld+json"
  148. />
  149. </head>
  150. <body>
  151. <ThemeProvider
  152. disableTransitionOnChange
  153. enableSystem
  154. attribute="class"
  155. defaultTheme="system"
  156. >
  157. <LocaleProvider lang={lang}>
  158. <a
  159. className="sr-only fixed top-3 left-3 z-[80] rounded-full bg-ink px-4 py-2 text-bg focus:not-sr-only"
  160. href="#main"
  161. >
  162. {t.meta.skip}
  163. </a>
  164. <Backdrop />
  165. <SiteNav />
  166. <main
  167. className="relative z-10 mx-auto w-full max-w-[1240px] grow px-3 pt-6 pb-6 sm:px-6 sm:pt-24 sm:pb-9"
  168. id="main"
  169. >
  170. {children}
  171. </main>
  172. <SiteFooter lang={lang} />
  173. <CursorLens />
  174. <UpdateOnReturn />
  175. </LocaleProvider>
  176. </ThemeProvider>
  177. <FontLoader faces={deferredFaces} />
  178. <Analytics />
  179. </body>
  180. </html>
  181. );
  182. }